package cn.wolfcode.trip.api.controller;

import cn.wolfcode.trip.base.domain.NewsSign;
import cn.wolfcode.trip.base.service.INewsSignService;
import cn.wolfcode.trip.base.util.JsonResult;
import cn.wolfcode.trip.base.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/newsSigns")
public class NewsSignController {

    @Autowired
    private INewsSignService newsSignService;

    /**
     * 新增标记
     * @param newsSign
     * @return
     */
    @PostMapping
    public JsonResult save(NewsSign newsSign) {
        JsonResult result = new JsonResult();
        //设置点赞或者收藏的时间
        newsSign.setUser(UserContext.getCurrentUser());
        try {
            newsSignService.save(newsSign);
            HashMap<Object, Object> map = new HashMap<>();
            int greatStatus = newsSignService.queryGreatStatusCountByNewsId(newsSign.getNews().getId());
            int enshrineStatus = newsSignService.queryEnshrineStatusCountByNewsId(newsSign.getNews().getId());
            map.put("greatStatus", greatStatus);
            map.put("enshrineStatus", enshrineStatus);
            result.setData(map);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    /**
     * 更新标记
     * @param newsSign
     * @return
     */
    @PutMapping("{id}")
    public JsonResult update(NewsSign newsSign) {
        JsonResult result = new JsonResult();
        newsSign.setUser(UserContext.getCurrentUser());
        try {
            newsSignService.update(newsSign);
            HashMap<Object, Object> map = new HashMap<>();
            int greatStatus = newsSignService.queryGreatStatusCountByNewsId(newsSign.getNews().getId());
            int enshrineStatus = newsSignService.queryEnshrineStatusCountByNewsId(newsSign.getNews().getId());
            map.put("greatStatus", greatStatus);
            map.put("enshrineStatus", enshrineStatus);
            result.setData(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 查询当前用户在日报下的标记状态
     *
     * @param newsId
     * @return
     */
    @GetMapping("{newsId}")
    public NewsSign selectByNewsId(@PathVariable Long newsId) {
        return newsSignService.selectByNewsId(newsId, UserContext.getCurrentUser().getId());
    }

    /**
     * 查询当前某个日报的点赞和收藏总数
     *
     * @param newsId
     * @return
     */
    @GetMapping("count/{newsId}")
    public Map queryGreatStatusCountByNewsId(@PathVariable Long newsId) {
        HashMap<Object, Object> map = new HashMap<>();
        int greatStatus = newsSignService.queryGreatStatusCountByNewsId(newsId);
        int enshrineStatus = newsSignService.queryEnshrineStatusCountByNewsId(newsId);
        map.put("greatStatus", greatStatus);
        map.put("enshrineStatus", enshrineStatus);
        return map;
    }


}
